package view.quest
{
import flash.display.MovieClip;
import flash.text.TextField;

import framework.core.AssetsManager;
import framework.core.FontLib;
import framework.utils.TextUtil;

import values.events.QuestValueEvent;
import values.quest.QuestGoalValue;
import values.quest.QuestValue;

/**
 * QuestDetailGoalRenderProxy
 * @author jliang
 * 
 */
public class QuestDetailGoalRenderProxy extends QuestComponentProxyBase
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function QuestDetailGoalRenderProxy()
	{
		super();
	}	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var index:int = 0;
	private var iconHolder:MovieClip;
	private var descLabel:TextField;
	private var progressLabel:TextField;
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get goalValue():QuestGoalValue
	{
		return index < data.goals.length ? data.goals[index] : null;
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		index = int(skin.name.split("_render_")[1]);
		
		iconHolder = skin.iconHolder;
		descLabel = skin.descLabel;
		progressLabel = skin.progressLabel;
	}
	
	override public function set data(value:QuestValue):void
	{
		super.data = value;
		
		clear();
	    if (!goalValue)
		{
			skin.visible = false;
		}
		else
		{
			skin.visible = true;
			
			iconHolder.addChild(AssetsManager.loadAsset(goalValue.template.icon));
			
			TextUtil.setText(descLabel, goalValue.template.desc, FontLib.DESC_FONT_NAME);
			TextUtil.setText(progressLabel, goalValue.progressString);
			
			value.addEventListener(QuestValueEvent.PROGRESS, questProgressHandler);
			
			checkUI();
		}
	}
	
	override public function dispose():void
	{
		if (data) data.removeEventListener(QuestValueEvent.PROGRESS, questProgressHandler);
		super.dispose();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function clear():void
	{
		while (iconHolder.numChildren > 0)
		{
			iconHolder.removeChildAt(0);
		}
		descLabel.text = "";
		progressLabel.text = "";
	}
	
	protected function checkUI():void
	{
		if (goalValue.isCompleted)
		{
			skin["completedIcon"].visible = true;
			progressLabel.visible = false;
		}
		else
		{
			skin["completedIcon"].visible = false;
			progressLabel.visible = true;
		}
	}
	
	protected function questProgressHandler(event:QuestValueEvent):void
	{
		checkUI();
	}
}
}



